<?php

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

    /**
     * Description of BeerList
     *
     * @author moon
     */
    class CashierList {
        //put your code here
        public $cashier_code;
        public $name;
    }

    include 'config.php';

    try {
        /*        $db = new PDO(
          "mysql:host=$db_host;dbname=$db_name;", $db_user, $db_pass
          );
         */
        $db = new PDO("odbc:$odbc_name", '', '');

        $db->query("SET NAMES 'utf8'");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        error_log($e->getMessage());
        die("A database error was encountered, maybe ODBC name $odbc_name was not set in the OS yet. (in file: " . __FILE__ . ')');
    }

    try {
        $query = 'select * from cashier order by cashier';
        $stmt = $db->prepare("$query");
        $stmt->execute();

        $json = array(
                'cashier'=>array(),
        );

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $cashier = new CashierList();
            $cashier->name = $row['name'];
            $cashier->cashier_code = $row['cashier'];
            $json['cashier'][] = $cashier;
        }
    }
    catch (PDOException $e) {
      print $e->getMessage();
    }
    unset($db);
    unset($stmt);

    $file = fopen('cashier.json', 'w');
    fwrite($file, json_encode($json));
    fclose($file);